package google.news;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import proxy.HttpsProxy;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhongwei on 2017/1/28.
 */
public class DownLoader {

    private HttpsProxy proxy;

    public String download(String url) {
        if (proxy == null) {
            changeProxy(url);
        }
        try {
            Document document = Jsoup.connect(url).validateTLSCertificates(false).userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36").header("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4").proxy(proxy.getHost(), proxy.getPort()).timeout(10000).get();
            return document.html();
        } catch (IOException ex) {
            changeProxy(url);
            return download(url);
        }
    }

    private void changeProxy(String url) {
        Pattern pattern = Pattern.compile("\"host\":\"(.+?)\",\"port\":(.+?),");
        try {
            HttpResponse response = HttpClients.createDefault().execute(new HttpGet("http://202.114.70.54:8081/get"));
            String content = EntityUtils.toString(response.getEntity());
            EntityUtils.consumeQuietly(response.getEntity());
            Matcher matcher = pattern.matcher(content);
            if (matcher.find()) {
                String host = matcher.group(1);
                int port = Integer.parseInt(matcher.group(2));
                this.proxy = new HttpsProxy(host, port);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
